<?php


function login($usr, $pwd){
	$response=new WsResponse();	
	if (session_login($usr,$pwd)){
		$response->setStatusCode("0");
		$response->setStatusMessage("Session established");		
	}else{
		$response->setStatusCode("-1");
		$response->setStatusMessage("Bad login");				
	}
	echo($response->toXml());
}

function logout(){
	session_logout();
	$response=new WsResponse();	
	$response->setStatusCode("0");
	$response->setStatusMessage("Session logged out");
	echo($response->toXml());
}

function getComposants(){
	$datastring="";	
	$cnx=dbconn();
	$sql = 'SELECT c.idComp, c.nomComp, c.descComp, c.imgComp, c.qteStockComp, c.qteAlerteComp FROM Composant c';
	$pq=$cnx->prepare($sql);
	$pq->execute();
	while ($row = $pq->fetch(PDO::FETCH_OBJ)) {
			$datastring=$datastring."<composant>";
			$datastring=$datastring."<idcomp>".$row->idComp."</idcomp>";
			$datastring=$datastring."<nomcomp>".$row->nomComp."</nomcomp>";
			$datastring=$datastring."<desccomp>".$row->descComp."</desccomp>";
			$datastring=$datastring."<imgcomp>".$row->imgComp."</imgcomp>";
			$datastring=$datastring."<qtestockcomp>".$row->qteStockComp."</qtestockcomp>";
			$datastring=$datastring."<seuilalertecomp>".$row->qteAlerteComp."</seuilalertecomp>";
			$datastring=$datastring."</composant>";
	}
	$response=new WsResponse();
	$response->setStatusCode("1");
	$response->setStatusMessage("OK");
	$response->setData($datastring);
	echo($response->toXml());
}

function getComposantID($id){
	$datastring="";	
	$cnx=dbconn();
	$sql = 'SELECT idComp, nomComp, descComp, imgComp, qteStockComp, qteAlerteComp FROM Composant WHERE idComp = ?';
	$pq=$cnx->prepare($sql);
	$pq->bindParam(1, $id);
	$pq->execute();
	$response=new WsResponse();	
	if ($pq->rowCount()!=1){
		$response->setStatusCode("-1");
		$response->setStatusMessage("Composant introuvable");
	}else{
		$row = $pq->fetch(PDO::FETCH_OBJ);
		$datastring=$datastring."<composant>";
		$datastring=$datastring."<idcomp>".$row->idComp."</idcomp>";
		$datastring=$datastring."<nomcomp>".$row->nomComp."</nomcomp>";
		$datastring=$datastring."<desccomp>".$row->descComp."</desccomp>";
		$datastring=$datastring."<imgcomp>".$row->imgComp."</imgcomp>";
		$datastring=$datastring."<qtestockcomp>".$row->qteStockComp."</qtestockcomp>";
		$datastring=$datastring."<seuilalertecomp>".$row->qteAlerteComp."</seuilalertecomp>";
		$datastring=$datastring."</composant>";	
		$response->setStatusCode("1");
		$response->setStatusMessage("OK");
		$response->setData($datastring);
	}
	echo($response->toXml());
}


function getComposantNom($nom){
	$datastring="";	
	$cnx=dbconn();
	$sql = 'SELECT idComp, nomComp, descComp, imgComp, qteStockComp, qteAlerteComp FROM Composant WHERE nomComp LIKE :str';	
	$pq=$cnx->prepare($sql);
	//$pq->bindParam(1,$nom);	
	$pq->execute(array(':str'=>'%'.$nom.'%'));
	$response=new WsResponse();
	if ($pq->rowCount()==0){
		//echec de l'appel
		$response->setStatusCode("-1");
		$response->setStatusMessage("Composant introuvable");
	}else{
		while ($row = $pq->fetch(PDO::FETCH_OBJ)) {
			$datastring=$datastring."<composant>";
			$datastring=$datastring."<idcomp>".$row->idComp."</idcomp>";
			$datastring=$datastring."<nomcomp>".$row->nomComp."</nomcomp>";
			$datastring=$datastring."<desccomp>".$row->descComp."</desccomp>";
			$datastring=$datastring."<imgcomp>".$row->imgComp."</imgcomp>";
			$datastring=$datastring."<qtestockcomp>".$row->qteStockComp."</qtestockcomp>";
			$datastring=$datastring."<seuilalertecomp>".$row->qteAlerteComp."</seuilalertecomp>";
			$datastring=$datastring."</composant>";
		}
		//succes de l'appel		
		$response->setStatusCode("1");
		$response->setStatusMessage("OK");
		$response->setData($datastring);
	}
	echo($response->toXml());
}

function addComposant($nom, $desc, $img, $qtStock, $qtAlerte){
	$cnx=dbconn();
	$sql = 'INSERT into Composant(idComp, nomComp, descComp, imgComp, qteStockComp, qteAlerteComp) values ( null, ?,?,?,?,?)';
	$pq=$cnx->prepare($sql);
	$pq->bindParam(1, $nom);
	$pq->bindParam(2, $desc);
	$pq->bindParam(3, $img);
	$pq->bindParam(4, $qtStock);
	$pq->bindParam(5, $qtAlerte);
	$response=new WsResponse();			
	if($pq->execute()){
		//succes de l'insert	
		$response->setStatusCode("1");
		$response->setStatusMessage("OK");
	}else{
		//echec de l'insert
		$response->setStatusCode("-1");
		$response->setStatusMessage("ECHEC AJOUT");
	}
	echo($response->toXml());
}

?>
